package com.tanhua.dubbo.api;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.OptionsMapper;
import com.tanhua.dubbo.mappers.QuestionsMapper;
import com.tanhua.dubbo.mappers.SoulPaperMapper;
import com.tanhua.dubbo.utils.AnimalsUtils;
import com.tanhua.model.db.Options;
import com.tanhua.model.db.Questions;
import com.tanhua.model.db.SoulPaper;
import com.tanhua.model.mongo.UserReport;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.sql.Wrapper;
import java.util.List;

@DubboService
public class SoulPaperApiImpl implements SoulPaperApi {

    @Autowired
    private SoulPaperMapper soulPaperMapper;
    @Autowired
    private QuestionsMapper questionsMapper;
    @Autowired
    private OptionsMapper optionsMapper;



    @Override//查询问卷
    public List<SoulPaper> findSoul() {
        //从数据库查询问卷对象,查询全部，没有条件
        return soulPaperMapper.selectList(null);

    }

    @Override//查询题目
    public List<Questions> findQuestions() {
        //查询全部问题
        return questionsMapper.selectList(null);

    }

    @Override//查询问题选项表
    public List<Options> findOptions() {
        //查询全部选项
        return optionsMapper.selectList(null);
    }

    @Override//根据选项生成报告
    public UserReport findReport(List<Long> optionIds,Long soulPaperId,Long userId) {
        UserReport report = new UserReport();
        report.setUserId(userId);
        //因为题目设计原因，相同选项的评分一样，所以这里只用选项id集合查询
        //1.构建条件
        QueryWrapper<Options> qw = new QueryWrapper<>();
        qw.in ("id",optionIds);
        //2.查询得到对应的选项集合
        List<Options> optionsList = optionsMapper.selectList(qw);
        //3.获取总分
        Integer sum=0;
        for (Options options : optionsList) {
            sum+=options.getScore();
        }
       //4.判断一下sum的值在哪个区间
        if (sum<21){
            report.setSoulPaperId(soulPaperId);//试卷id
            report.setScope(0+","+AnimalsUtils.MAX_OWL);//猫头鹰的范围
            report.setGenre(AnimalsUtils.ANIMAL_OWL);   //类型
            report.setCover(AnimalsUtils.URL_OWL);
            report.setConclusion(AnimalsUtils.EVALUATE_OWL);
        }else if(estimate(sum,AnimalsUtils.MAX_RABBIT,AnimalsUtils.MIN_RABBIT)){
            report.setSoulPaperId(soulPaperId);//试卷id
            report.setScope(AnimalsUtils.MIN_RABBIT+","+AnimalsUtils.MAX_RABBIT);//兔子的范围
            report.setGenre(AnimalsUtils.ANIMAL_RABBIT);   //类型
            report.setCover(AnimalsUtils.URL_RABBIT);
            report.setConclusion(AnimalsUtils.EVALUATE_RABBIT);
        }else if(estimate(sum,AnimalsUtils.MAX_FOX,AnimalsUtils.MIN_FOX)){
            report.setSoulPaperId(soulPaperId);//试卷id
            report.setScope(AnimalsUtils.MIN_FOX+","+AnimalsUtils.MAX_FOX);//狐狸的范围
            report.setGenre(AnimalsUtils.ANIMAL_FOX);   //类型
            report.setCover(AnimalsUtils.URL_FOX);
            report.setConclusion(AnimalsUtils.EVALUATE_FOX);
        }else{
            report.setSoulPaperId(soulPaperId);//试卷id
            report.setScope(AnimalsUtils.MIN_LION+","+100);//狮子的范围
            report.setGenre(AnimalsUtils.ANIMAL_LION);   //类型
            report.setCover(AnimalsUtils.URL_LION);
            report.setConclusion(AnimalsUtils.EVALUATE_LION);
        }
        return report;

    }

    //封装一个方法，即判断数组number是否在min和max之间
    public static boolean estimate(int number, int min, int max) {
        return Math.max(min, number) == Math.min(number, max);
    }

}
